package com.itheima.web.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.db.Log;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.*;
import com.itheima.service.db.LogService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.db.UserService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.service.mongo.VideoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserManager {
    @Reference
    private UserInfoService userInfoService;
    @Reference
    private UserService userService;
    @Reference
    private LogService logService;
    @Reference
    private UserLikeService userLikeService;
    /**
     * 用户列表数据
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageBeanVo findUsers(Integer pageNumber, Integer pageSize) {
        //分页查询
        PageBeanVo pageBeanVo = userInfoService.findUsers(pageNumber, pageSize);
        return pageBeanVo;
    }

    /**
     * 查看用户详情
     * @param userId
     * @return
     */
    public UserDetailVo findUserById(Long userId) {

        UserInfo userInfo = userInfoService.findById(userId);

        User user = userService.fingById(userId);

        Log log = logService.findByUserId(userId);

        Long eachLoveCount = userLikeService.findEachLoveCount(userId);//互相喜欢
        Long fanCount = userLikeService.findFanCount(userId);//喜欢我
        Long loveCount = userLikeService.findLoveCount(userId);//我喜欢

        UserDetailVo vo = new UserDetailVo();
        vo.setUserInfo(userInfo);
        vo.setUser(user);
        vo.setLog(log);
        vo.setFriendCount(eachLoveCount);
        vo.setLikeMeCount(fanCount);
        vo.setLikeCount(loveCount);
        return vo;
    }


    @Reference
    private VideoService videoService;
    /**
     * 分页查询视频列表
     */
    public PageBeanVo findVideoByUserId(Integer pageNumber, Integer pageSize, Long uid) {
        //1.调用分页方法查询数据
        PageBeanVo pageBeanVo = videoService.findVideoByUid(pageNumber ,pageSize ,uid );
        //2.构建VideoVo对象
        List<Video> items = (List<Video>) pageBeanVo.getItems();
        List<VideoVo> videoVoList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(items)){
            for (Video video : items) {
                VideoVo vo = new VideoVo();
                UserInfo userInfo = userInfoService.findById(video.getUserId());
                //转换
                BeanUtil.copyProperties(userInfo ,vo );
                BeanUtil.copyProperties(video ,vo );
                //赋值不一样的字段
                //封面
                vo.setCover(video.getPicUrl());
                //签名
                vo.setSignature(video.getText());
                videoVoList.add(vo);
            }
        }
        //赋值数据
        pageBeanVo.setItems(videoVoList);
        return pageBeanVo;
    }


    @Reference
    private MovementService movementService;


    /**
     * 根据传入的用户id和需要查询的动态状态 查询数据封装成vo对象
     * @param pageNumber
     * @param pageSize
     * @param uid
     * @param startDate
     * @param endDate
     * @return
     */
    public PageBeanVo findMovementList(Integer pageNumber, Integer pageSize, Long uid, Long startDate,Long endDate) {

        //分页查询动态数据
        PageBeanVo pageBeanVo = movementService.findMovementsByUserIdAndState(pageNumber, pageSize, uid, startDate, endDate);

        List<Movement> items = (List<Movement>) pageBeanVo.getItems();
        List<MovementVo> voList = new ArrayList<>();
        //转换
        if(CollectionUtil.isNotEmpty(items)){
            for (Movement movement : items) {
                //查询用户的基础数据
                UserInfo userInfo = userInfoService.findById(movement.getUserId());
                //封装数据
                MovementVo vo = new MovementVo();
                vo.setUserInfo(userInfo);
                vo.setMovement(movement);
                //时间的转换
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(movement.getCreated());//将时间戳转换成日期
                String format =simpleDateFormat.format(date);
                vo.setCreateDate(format);
                voList.add(vo);
            }
        }
        pageBeanVo.setItems(voList);
        return pageBeanVo;
    }

    /**
     * 动态详情
     * @param movementId
     * @return
     */
    public MovementVo findMovementById(String movementId) {
        Movement movement = movementService.findMovementById(movementId);
        MovementVo vo = new MovementVo();

        if(movement != null){
            //查询用户的基础数据
            UserInfo userInfo = userInfoService.findById(movement.getUserId());
            //封装数据
            vo.setUserInfo(userInfo);
            vo.setMovement(movement);
            //时间的转换
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(movement.getCreated());//将时间戳转换成日期
            String format =simpleDateFormat.format(date);
            vo.setCreateDate(format);

        }

        return vo;
    }

    @Reference
    private CommentService commentService;
    /**
     * 分页查询评论列表
     * @param movementId
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageBeanVo findCommentList(String movementId, Integer pageNumber, Integer pageSize) {
        //查询评论列表数据
        PageBeanVo pageBeanVo = commentService.findMovementComment(movementId , pageNumber , pageSize);
        List<Comment> items = (List<Comment>) pageBeanVo.getItems();
        List<CommentVo> voList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(items)){
            for (Comment comment : items) {
                //封装
                CommentVo vo = new CommentVo();
                //用户信息
                UserInfo userInfo = userInfoService.findById(comment.getUserId());

                vo.setId(comment.getId().toHexString());//评论id
                vo.setAvatar(userInfo.getAvatar());//头像
                vo.setNickname(userInfo.getNickname());//昵称
                vo.setContent(comment.getContent());//评论

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(comment.getCreated());//将时间戳转换成日期
                String format =simpleDateFormat.format(date);
                vo.setCreateDate(format);
                voList.add(vo);
            }
        }
        pageBeanVo.setItems(voList);
        return pageBeanVo;
    }
}
